package com._9xcloud.leaverequest.service;

import com._9xcloud.leaverequest.domain.Clazz;
import com._9xcloud.leaverequest.domain.Student;
import com._9xcloud.leaverequest.exception.InvalidPasswordException;
import com._9xcloud.leaverequest.exception.NotFoundException;
import com._9xcloud.leaverequest.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;

/**
 * Created by hyq on 2015/5/10.
 */

@Repository
@Transactional
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;

    @Transactional(readOnly = true)
    public Student authenticate(String username, String password) throws AuthenticationException {
        Student student = loadUserByUsername(username);
        if (!student.getPassword().contentEquals(password)) {
            throw new BadCredentialsException("Invalid password");
        }
        return student;
    }

    @Transactional(readOnly = true)
    public Student loadUserByUsername(String username) throws UsernameNotFoundException {
        Student student = studentRepository.findByUsername(username);
        if (student == null) {
            throw new UsernameNotFoundException("Could not find the user");
        }
        return student;
    }

    @Transactional(readOnly = true)
    public Student getById(int studentId) {
        Student student = studentRepository.findOne(studentId);
        if (student == null) {
            throw new NotFoundException("Could not find the user");
        }
        return student;
    }

    public void changePassword(int studentId, String oldPassword, String newPassword) {
        Student student = getById(studentId);
        if (!student.getPassword().contentEquals(oldPassword)) {
            throw new InvalidPasswordException("The old password is incorrect");
        }
        if (StringUtils.isEmpty(newPassword)) {
            throw new InvalidPasswordException("The new password can not be empty");
        }
        student.setPassword(newPassword);
        studentRepository.saveAndFlush(student);
    }

    public void removeAll() {
        studentRepository.deleteAll();
        studentRepository.flush();
    }

    public Student create(String fullName, String username, String password, Clazz clazz) {
        Student student = new Student(fullName, username, password, clazz);
        return studentRepository.saveAndFlush(student);
    }

    @Transactional(readOnly = true)
    public List<Student> getAll() {
        return studentRepository.findAll();
    }

    public void update(Student student) {
        studentRepository.saveAndFlush(student);
    }

    public Set<Student> getByName(String name) {
        return studentRepository.findByFullNameLike(name);
    }

    @Transactional(readOnly = true)
    public Set<Student> getByClazzId(int clazzId) {
        return studentRepository.findByClazzId(clazzId);
    }
}
